/**
 * DeviceBookService.java
 * com.ebook.server.service
 * Copyright (c) 2013, 公司版权所有.
*/

package com.ebook.server.service;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ebook.server.dao.DeviceBookDao;

/**
 * 用户设备_图书关系service层
 * @author    潘吉友
 * @Date	 2013-6-5 	 
 */
@Service
@Transactional
public class DeviceBookService {

	@Resource
	private JdbcTemplate jdbcTemplate;
	
	private static Log logger = LogFactory.getLog(DeviceBookService.class);
	
	@Resource
	private DeviceBookDao deviceBookDao;

	
	public boolean add(String device_id, int bookid, String keys) {
		if (isExist(device_id, bookid)) {
			deviceBookDao.updateKeys(device_id, bookid, keys);
		} else {
			deviceBookDao.add(device_id, bookid, keys);
		}
		return true;
	}

	public boolean isExist(String device_id,int bookid){
		return deviceBookDao.isExist(device_id, bookid);
	}
	
	public void delete(String device_id){
		deviceBookDao.delete(device_id);
	}
	
	public String findKeys(String device_id,int bookid){
		return deviceBookDao.findKeys(device_id, bookid);
	}
	
	public long findAfterSyncTime(String device_id,int bookid){
		return deviceBookDao.findAfterSyncTime(device_id, bookid);
	}
	
	/**
	 * 获取同步时间
	 * @param device_id
	 * @param bookid
	 * @return 同步时间（-1：没找到）
	*/
	public long findNoteSyncTime(String device_id,int bookid){
		return deviceBookDao.findNoteSyncTime(device_id, bookid);
	}
	
	/**
	 *获取同一用户不同设备同一本书最小的更新时间
	 * @param uid
	 * @param bookid
	 * @return 最小的更新时间
	*/
	public long findNoteSyncTimeBySameUser(int uid,int bookid){
		String sql = "select min(db.note_sync_time) as min_note_sync_time from device_book as db,device_user as du " +
				"where du.device_id=db.device_id and du.user_id=? and db.bookid=? ";
		try{
			logger.info("|findNoteSyncTimeBySameUser|"+sql);
			Map<String,Object> map = jdbcTemplate.queryForMap(sql,uid,bookid);
			return (Long)(map.get("min_note_sync_time"));
		}catch(Exception e){
			return -1;
		}
	}
	
	public void updateNoteSyncTime(long note_sync_time,String device_id,int bookid){
		deviceBookDao.updateNoteSyncTime(note_sync_time, device_id, bookid);
	}
	
	public void updateAfterSyncTime(long after_sync_time,String device_id,int bookid){
		deviceBookDao.updateAfterSyncTime(after_sync_time, device_id, bookid);
	}

	public int countOnlineSameBookByCnd(int uid,int bookid,String device_id,long readingTime){
		String sql = "select count(*) from device_user as du,device_book as db " +
				"where du.device_id=db.device_id and du.user_id=? and db.bookid=? and db.device_id!=? and db.reading_time>?";
		return jdbcTemplate.queryForInt(sql,uid,bookid,device_id,readingTime);
	}
	
	public void updateReadingTime(int bookid,String device_id,long readingTime){
		deviceBookDao.updateReadingTime(bookid, device_id, readingTime);
	}
}

